<template>
  <div class="profile-container">
    <div class="profile-header">
      <div class="avatar-container">
        <img :src="userInfo.avatar" alt="用户头像" class="avatar" v-if="userInfo.avatar">
        <div class="avatar-placeholder" v-else>
          {{ userInfo.name ? userInfo.name.charAt(0) : '?' }}
        </div>
        <button class="edit-avatar-btn" @click="editAvatar">更换头像</button>
      </div>
      <h1 class="username">{{ userInfo.name }}</h1>
      <p class="school-info">{{ userInfo.school }} · {{ userInfo.college }}</p>
    </div>

    <div class="profile-content">
      <div class="profile-section">
        <h2 class="section-title">基本信息</h2>
        <div class="info-item">
          <span class="info-label">姓名</span>
          <span class="info-value">{{ userInfo.name }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">学校</span>
          <span class="info-value">{{ userInfo.school }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">学院</span>
          <span class="info-value">{{ userInfo.college }}</span>
        </div>
      </div>

      <div class="profile-section">
        <h2 class="section-title">联系方式</h2>
        <div class="info-item">
          <span class="info-label">电话</span>
          <span class="info-value">{{ userInfo.phone }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">邮箱</span>
          <span class="info-value">{{ userInfo.email }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">家庭地址</span>
          <span class="info-value">{{ userInfo.address }}</span>
        </div>
      </div>

      <div class="profile-section">
        <h2 class="section-title">母校行活动经历</h2>
        <div class="experience-list">
          <div class="experience-item" v-for="(exp, index) in userInfo.experiences" :key="index">
            <div class="experience-header">
              <h3 class="experience-title">{{ exp.teamName }}</h3>
              <span class="experience-time">{{ exp.time }}</span>
            </div>
            <div class="experience-location">{{ exp.location }}</div>
            <div class="experience-desc">{{ exp.description }}</div>
          </div>
          <button class="add-experience-btn" @click="addExperience" v-if="!isAddingExperience">
            + 添加经历
          </button>

          <div class="experience-form" v-if="isAddingExperience">
            <h3>添加新经历</h3>
            <div class="form-group">
              <label>队伍名称</label>
              <input type="text" v-model="newExperience.teamName" placeholder="请输入队伍名称">
            </div>
            <div class="form-group">
              <label>参与时间</label>
              <input type="text" v-model="newExperience.time" placeholder="例如：2023年1月">
            </div>
            <div class="form-group">
              <label>参与地点</label>
              <input type="text" v-model="newExperience.location" placeholder="请输入活动地点">
            </div>
            <div class="form-group">
              <label>活动描述</label>
              <textarea v-model="newExperience.description" placeholder="请描述活动经历"></textarea>
            </div>
            <div class="form-actions">
              <button class="cancel-btn" @click="cancelAddExperience">取消</button>
              <button class="save-btn" @click="saveExperience">保存</button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="profile-actions">
      <button class="edit-profile-btn" @click="editProfile">编辑个人信息</button>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const userInfo = ref({
      avatar: 'https://randomuser.me/api/portraits/men/1.jpg',
      name: '林安芯',
      school: '锦江学院',
      college: '大数据技术',
      phone: '18227133073',
      email: '3481511233@qq.com',
      address: '四川省德阳市旌阳区',
      experiences: [
        {
          teamName: '2023寒假母校行小分队',
          time: '2023年1月',
          location: '德阳市第一中学',
          description: '作为队长组织了回访母校活动，向学弟学妹介绍大学生活。'
        },
      ]
    });

    const isAddingExperience = ref(false);
    const newExperience = ref({
      teamName: '',
      time: '',
      location: '',
      description: ''
    });

    const editAvatar = () => {
      alert('头像编辑功能待实现');
    };

    const editProfile = () => {
      alert('个人信息编辑功能待实现');
    };

    const addExperience = () => {
      isAddingExperience.value = true;
    };

    const cancelAddExperience = () => {
      isAddingExperience.value = false;
      resetNewExperience();
    };

    const saveExperience = () => {
      if (!newExperience.value.teamName || !newExperience.value.time || !newExperience.value.location) {
        alert('请填写必填字段');
        return;
      }

      userInfo.value.experiences.unshift({...newExperience.value});
      isAddingExperience.value = false;
      resetNewExperience();
    };

    const resetNewExperience = () => {
      newExperience.value = {
        teamName: '',
        time: '',
        location: '',
        description: ''
      };
    };

    return {
      userInfo,
      isAddingExperience,
      newExperience,
      editAvatar,
      editProfile,
      addExperience,
      cancelAddExperience,
      saveExperience
    };
  }
};
</script>

<style scoped>
.profile-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Helvetica Neue', Arial, sans-serif;
  color: #333;
}

.profile-header {
  text-align: center;
  margin-bottom: 30px;
}

.avatar-container {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 15px;
}

.avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.avatar-placeholder {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #4a89dc;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  font-weight: bold;
  border: 3px solid #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.edit-avatar-btn {
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  border: 1px solid #ddd;
  border-radius: 15px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.username {
  font-size: 24px;
  margin: 10px 0 5px;
  color: #333;
}

.school-info {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.profile-content {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.profile-section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 18px;
  color: #4a89dc;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
  margin-bottom: 15px;
}

.info-item {
  display: flex;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px dashed #f0f0f0;
}

.info-label {
  width: 100px;
  color: #666;
  font-weight: 500;
}

.info-value {
  flex: 1;
  color: #333;
}

.experience-list {
  margin-top: 15px;
}

.experience-item {
  background: #f9f9f9;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 15px;
}

.experience-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.experience-title {
  font-size: 16px;
  margin: 0;
  color: #333;
}

.experience-time {
  color: #666;
  font-size: 14px;
}

.experience-location {
  color: #4a89dc;
  font-size: 14px;
  margin-bottom: 8px;
}

.experience-desc {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
}

.add-experience-btn {
  width: 100%;
  padding: 10px;
  background: #f0f0f0;
  border: 1px dashed #ccc;
  border-radius: 6px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.add-experience-btn:hover {
  background: #e0e0e0;
}

.experience-form {
  background: #f5f7fa;
  padding: 15px;
  border-radius: 6px;
  margin-top: 15px;
}

.experience-form h3 {
  margin-top: 0;
  margin-bottom: 15px;
  color: #333;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #555;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-family: inherit;
}

.form-group textarea {
  min-height: 80px;
  resize: vertical;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn,
.save-btn {
  padding: 8px 15px;
  border-radius: 4px;
  cursor: pointer;
}

.cancel-btn {
  background: #f0f0f0;
  border: 1px solid #ddd;
  color: #666;
}

.save-btn {
  background: #4a89dc;
  border: 1px solid #3a79d4;
  color: white;
}

.profile-actions {
  margin-top: 20px;
  text-align: center;
}

.edit-profile-btn {
  padding: 10px 20px;
  background: #4a89dc;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.3s;
}

.edit-profile-btn:hover {
  background: #3a79d4;
}

@media (max-width: 600px) {
  .profile-container {
    padding: 15px;
  }

  .info-item {
    flex-direction: column;
  }

  .info-label {
    margin-bottom: 5px;
    width: auto;
  }
}
</style>